package org.wenhuayuan.library.util

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.WindowManager

object HiStatusBar {

    /**
     * @param darkContent 指：白底黑字
     * @param statusBarColor 指： 状态栏背景色
     * @param transient 指： 沉浸式效果，即页面布局延申到状态栏之下
     */
    fun setStatusBar (
        activity: Activity,
        darkContent: Boolean,
        statusBarColor: Int = Color.WHITE,
        transient: Boolean
    ) {

        val window = activity.window
        val decorView = window.decorView
        var visibility = decorView.systemUiVisibility

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 请求系统绘制状态栏的背景色:FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS和FLAG_TRANSLUCENT_STATUS不能同时出现
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

            window.statusBarColor = statusBarColor
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            visibility = if (darkContent) {
                // 白底黑字 - - 浅色主题
                visibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                // 黑底白字 - - 深色主题
                // java写法：visibility &= ~ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                // 去除 SYSTEM_UI_FLAG_LIGHT_STATUS_BAR的标记
                visibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
            }
        }

        if (transient) {
            // 此时 能够使得页面的布局延申到状态栏之下，但是状态栏图标就看不见了
            // visibility = visibility or View.SYSTEM_UI_FLAG_FULLSCREEN
            // 需要添加标记SYSTEM_UI_FLAG_LAYOUT_STABLE来恢复可见
            visibility = visibility or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }

        decorView.systemUiVisibility = visibility // 更新

    }
}